<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*会员卡管理*/
class Member extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	/*会员卡列表*/
	function index(){
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	//新增
	function add(){
		if(request()->isPost()){
			       $card_no=input("card_no");
			       $count=db("Interface")->where("card_no='$card_no'")->count(1);
				   if($count>0){
				   	   $this->ajax_error("卡号已经存在");
				   }
					$member=array(
					   			"card_no"=>$card_no,
					   			"in_varifycation"=>input("in_varifycation"),
								"start_date"=>input("start_date"),
								"end_date"=>input("end_date"),
								"mobile"=>input("mobile"),
								"contract_id"=>input("contract_id"),
								"member_id"=>input("member_id"),
								"name"=>input("name"),
								"surplus_num"=>input("surplus_num"),
								"membership_type"=>input("membership_type"),
								"contract_status"=>input("contract_status"),
								"owner_type"=>input("owner_type"),
					   );
				if(input("in_varifycation")==1){//需要验证
					if(input("in_varifycation_type")==1){//指纹验证
					    $member["fingerprint"]=input("fingerprint");
						$member['in_varifycation_type']=1;
					}else if(input("in_varifycation_type")==2){//人脸验证
						  $this->_face_code_insertOrupdate(array(
						  							"card_no"=>$card_no,"face1"=>input("face1"),
						  							"face2"=>input("face2"),"face3"=>input("face3"),
						  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
						  							"icon3"=>input("icon3"),
						  							));
													$member['in_varifycation_type']=2;
					}else if(input("in_varifycation_type")==3){//前台验证
						$member['in_varifycation_type']=3;
					}	
				}else{//不需要验证
					$member['in_varifycation_type']=0;
				}   
			db("Interface")->insert($member);
			return $this->ajax_success("成功");
		}else{
		  echo $this->fetch();die;
		}
	}
  	/**
	 * 修改会员
	 */
	function edit(){
		if(request()->isPost()){
		   $model=db("Interface")->find(input("id"));
		   $model["in_varifycation"]=input("in_varifycation");
		  
		   if(input("in_varifycation")==1){//需要验证
				if(input("in_varifycation_type")==1){//指纹验证
					$model['fingerprint']=input("fingerprint");
			  	    $model['in_varifycation_type']=1;
				}else if(input("in_varifycation_type")==2){//人脸验证
					 $this->_face_code_insertOrupdate(array(
							  							"card_no"=>$model['card_no'],"face1"=>input("face1"),
							  							"face2"=>input("face2"),"face3"=>input("face3"),
							  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
							  							"icon3"=>input("icon3"),
							  							));
					$model['in_varifycation_type']=2;									
				}else if(input("in_varifycation_type")==3){//前台验证
					$model['in_varifycation_type']=3;									
				}	
			}else{
				$model['in_varifycation_type']=0;
			}
			
			db("Interface")->update($model);
		   return $this->ajax_success("成功");
		
		}else{
			$model=db("Interface")->find(input("id"));
			$this->assign("model",$model);
			$facecode=db("FaceCode")->where("card_no='".$model['card_no']."'")->find();
			if(empty($facecode)){
				$this->assign("is_bind_face",0);
				$facecode=array("face1"=>null,"face2"=>null,"face3"=>null,"icon1"=>null,"icon2"=>null,"icon3"=>null);
			}else{
				$this->assign("is_bind_face",1);
			}
			if(empty($model['fingerprint'])){
				$this->assign("is_bind_finger",0);
			}else{
				$this->assign("is_bind_finger",1);
			}
			
			$this->assign("facecode",$facecode);
			echo $this->fetch();
		}
	}
	///制匹配卡
	function fetchcard(){
		$id=input("id");
		$this->assign("card_no",db("Interface")->where("id=$id")->value("card_no"));
		echo $this->fetch();
	}
	
	////查询卡
	function find_bycard(){
		$card_no=input("card_no");
		$model=db("interface")->where("card_no=$card_no")->find();
		if(empty($model)){
			return $this->ajax_error("抱歉！无法查找到会员。");
		}
		$this->ajax_success("成功",$model);
	}
	//查询
	function query(){
	    $card_no=input("card_no");
		$model=db("interface")->where("card_no='$card_no'")->find();
		if(empty($model)){
			$this->ajax_error("无效的卡号");
		}else{
			if($model["owner_type"]==1&&$model["in_varifycation"]==1&&$model["in_varifycation_type"]==3){
				$count=db("VarifyRecord")->where("card_no='$card_no' and is_in=0 ")->count(1);
				if($count>0){
					$this->ajax_error("此卡已经验证，请勿重复验证");
				}
			}else{
				$this->ajax_error("此卡不需要前台验证");	
			}
		}
		$this->ajax_success("成功",$model);	
	}
	
	/*前台验卡*/
	function varify(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$model=db("interface")->where("card_no='$card_no'")->find();
			if(empty($model)){
				$this->ajax_error("无效的卡号");
			}else{
				if($model["owner_type"]==1&&$model["in_varifycation"]==1&&$model["in_varifycation_type"]==3){
					$count=db("VarifyRecord")->where("card_no='$card_no' and is_in=0 ")->count(1);
					if($count>0){
						$this->ajax_error("此卡已经验证，请勿重复验证");
					}else{
						db("VarifyRecord")->insert(array("card_no"=>$card_no,"is_in"=>0));
						$this->ajax_success("成功");
					}
				}else{
					$this->ajax_error("此卡不需要前台验证");	
				}
			}
		}else{
			echo $this->fetch();
		}
	}
	//同步会员数据
	function sync(){
		if(request()->isPost()){
			$member=array(
					   			"id"=>input("id"),
								"start_date"=>input("start_date"),
								"end_date"=>input("end_date"),
								"mobile"=>input("mobile"),
								"contract_id"=>input("contract_id"),
								"member_id"=>input("member_id"),
								"name"=>input("name"),
								"surplus_num"=>input("surplus_num"),
								"membership_type"=>input("membership_type"),
								"contract_status"=>input("contract_status"),
			);
			db("Interface")->update($member);
			$this->ajax_success($member);
		}else{
			$model=$this->assign("model",db("Interface")->find(input("id")));
			echo $this->fetch();
		}
	}
	//离线新增
	function add_offline(){
		if(request()->isPost()){
			       $card_no=input("card_no");
			       $count=db("Interface")->where("card_no='$card_no'")->count(1);
				   if($count>0){
				   	   $this->ajax_error("卡号已经存在");
				   }
					$member=array(
					   			"card_no"=>$card_no,
					   			"in_varifycation"=>input("in_varifycation"),
								"start_date"=>input("start_date"),
								"end_date"=>input("end_date"),
								"mobile"=>input("mobile"),
								"contract_id"=>input("contract_id"),
								"member_id"=>input("member_id"),
								"name"=>input("name"),
								"surplus_num"=>input("surplus_num"),
								"membership_type"=>input("membership_type"),
								"contract_status"=>input("contract_status"),
								"owner_type"=>input("owner_type"),
					   );
				if(input("in_varifycation")==1){//需要验证
					if(input("in_varifycation_type")==1){//指纹验证
					    $member["fingerprint"]=input("fingerprint");
						$member['in_varifycation_type']=1;
					}else if(input("in_varifycation_type")==2){//人脸验证
						  $this->_face_code_insertOrupdate(array(
						  							"card_no"=>$card_no,"face1"=>input("face1"),
						  							"face2"=>input("face2"),"face3"=>input("face3"),
						  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
						  							"icon3"=>input("icon3"),
						  							));
													$member['in_varifycation_type']=2;
					}else if(input("in_varifycation_type")==3){//前台验证
						$member['in_varifycation_type']=3;
					}	
				}else{//不需要验证
					$member['in_varifycation_type']=0;
				}   
			db("Interface")->insert($member);
			return $this->ajax_success("成功");
		}else{
		  echo $this->fetch();die;
		}
	} 
	//离线编辑
	function edit_offline(){
		if(request()->isPost()){
		   $model=db("Interface")->find(input("id"));
		   $model["in_varifycation"]=input("in_varifycation");
		  
		   if(input("in_varifycation")==1){//需要验证
				if(input("in_varifycation_type")==1){//指纹验证
					$model['fingerprint']=input("fingerprint");
			  	    $model['in_varifycation_type']=1;
				}else if(input("in_varifycation_type")==2){//人脸验证
					 $this->_face_code_insertOrupdate(array(
							  							"card_no"=>$model['card_no'],"face1"=>input("face1"),
							  							"face2"=>input("face2"),"face3"=>input("face3"),
							  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
							  							"icon3"=>input("icon3"),
							  							));
					$model['in_varifycation_type']=2;									
				}else if(input("in_varifycation_type")==3){//前台验证
					$model['in_varifycation_type']=3;									
				}	
			}else{
				$model['in_varifycation_type']=0;
			}
			if($model['card_no']!=input('card_no')){
				 $count=db("Interface")->where("card_no='".input('card_no')."'"." and card_no!='".$model['card_no']."'")->count(1);
				   if($count>0){
				   	  return $this->ajax_error("卡号已经存在");
				   }else{
				    	$model["card_no"]=trim(input("card_no"));
				   }
			}
			$model["start_date"]=input("start_date");
			$model["end_date"]=input("end_date");
			$model["mobile"]=input("mobile");
			$model["name"]=input("name");
			$model["surplus_num"]=input("surplus_num");
			$model["membership_type"]=input("membership_type");
			$model["contract_status"]=input("contract_status");
			db("Interface")->update($model);
		   return $this->ajax_success("成功");
		
		}else{
			$model=db("Interface")->find(input("id"));
			$this->assign("model",$model);
			$facecode=db("FaceCode")->where("card_no='".$model['card_no']."'")->find();
			if(empty($facecode)){
				$this->assign("is_bind_face",0);
				$facecode=array("face1"=>null,"face2"=>null,"face3"=>null,"icon1"=>null,"icon2"=>null,"icon3"=>null);
			}else{
				$this->assign("is_bind_face",1);
			}
			if(empty($model['fingerprint'])){
				$this->assign("is_bind_finger",0);
			}else{
				$this->assign("is_bind_finger",1);
			}
			
			$this->assign("facecode",$facecode);
			echo $this->fetch();
		}
	}
	
    /*会员借卡*/
	function  borrow(){
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			$count=db("Interface")->where("card_no='$card_no'")->count(1);
			if($count>0){
				return $this->ajax_error("此卡号已经存在");
			}
			//新增一条借记卡
			db("Interface")->insert(array("card_no"=>input("card_no"),"name"=>input("name"),"mobile"=>input("mobile"),"is_original"=>0,"owner_type"=>1,"descs"=>input("descs"),"original_id"=>input("id")));
			//新增一条记录
			db("BorrowCard")->insert(array("card_no"=>input("card_no"),"name"=>input("name"),"mobile"=>input("mobile"),"descs"=>input("descs"),"original_card_no"=>input("original_card_no"),"owner_type"=>1));
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			$count=db("Interface")->where("original_id=$id")->count(1);
			if($count>0){// 存在借卡
			    echo "此会员已经借卡一张，请先归还";	
			    die;
			}
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	
	/*会员还卡*/
	function  back(){
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			//修改借记状态
			db("BorrowCard")->where("card_no='$card_no' and is_back=0 ")->update(array("back_time"=>date("Y-m-d H:i:s"),"is_back"=>1));
			//删除借记卡
			db("Interface")->where("card_no='$card_no' ")->delete();
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			//借记的卡号
			$borrow_card_no=db("Interface")->where("original_id=$id")->value("card_no");
			if(empty($borrow_card_no)){
			    echo "此会员没有借卡，无须还卡";	
			    die;
			}
			$this->assign("borrow_card_no",$borrow_card_no);
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	/*权限*/
	function auth(){
		if(request()->isPost()){
			$id=input("id");
			$door_ids=$_POST['door_ids'];
			if(empty($door_ids)){
				$door_ids=[0];
			}
				$door_ids=implode(",", $door_ids);
				$list=db("SlidingDoor")->where("id in($door_ids)")->column("code");
				db("Interface")->where("id=$id")->update(array("doors_auth"=>$door_ids));
				$this->ajax_success("成功");
		}else{
			$model=db("Interface")->find(input("id"));
			$this->assign("model",$model);
			$this->assign("doors_auth",$model['doors_auth']);
			$this->assign("doors",db("SlidingDoor")->select());
			echo $this->fetch(); die;
		}
	}
	
	//删除卡
	function delete(){
			$id=input("id");
			$count=db("BorrowCard")->where("card_no=(select card_no from m_interface where id=$id and is_back=0)")->find();
			if($count>0){
			  return	$this->ajax_error("请先还卡");
			}
			db("Interface")->where("id=$id")->delete();
			return $this->ajax_success("成功");
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041003");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'card_no'=>array("field" =>"card_no","operator" =>"like"),
					 'name'=>array("field" =>"name","operator" =>"like"),
					 'mobile'=>array("field" =>"mobile","operator" =>"like"),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and owner_type=1 and is_original=1 ";
		//借计卡条件
		if(!empty(input("borrow_card_no"))){
			$original_id=db("Interface")->where(" is_original=0 and owner_type=1 and card_no='".input("borrow_card_no")."'")->value("original_id");
			if(empty($original_id)){
				$original_id=0;
			}
			$where_and.=" and id=".$original_id;
		}
		
		$total=db("interface")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("interface")->alias("a")->field("fingerprint",true)->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		$borrow_list=$this->borrow_list();
		foreach ($list as $key => $value) {
			if(isset($borrow_list[$value['id']])){
				$list[$key]["borrow_card_no"]=$borrow_list[$value['id']];
			}else{
				$list[$key]["borrow_card_no"]="";
			}
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	//插入或者修改facecode
	private function _face_code_insertOrupdate($data){
		if(empty($data)){
			return;
		}else{
			$card_no=$data["card_no"];
			$model=db("FaceCode")->where("card_no='$card_no'")->find();
			if(empty($model)){//新增
				db("FaceCode")->insert($data);
			}else{//修改
				db("FaceCode")->update(array_merge($model,$data));
			}
			return;
		}
		
	}
	//获取会员借卡列表
	public function borrow_list(){
		return db("Interface")->where("owner_type=1 and is_original=0")->column("original_id,card_no");
	}
}
